using System.Collections.Generic;
using UnityEngine;
using YooAsset;

namespace LFramework 
{
    public class GameObjectPoolEntity
    {
        /// <summary>
        /// 加载出来的asset Prefab
        /// </summary>
        private GameObject Prefab;
        /// <summary>
        /// 实例列表
        /// </summary>
        private LinkedList<GameObject> InstanceList = new LinkedList<GameObject>();

        /// <summary>
        /// 出池子
        /// </summary>
        /// <param name="namePath"></param>
        /// <param name="instanceComplete"></param>
        public GameObject DeSpawn(string assetFullName, Transform parent = null)
        {
            GameObject obj = null;
            if (Prefab != null)
            {
                if (InstanceList.First == null)
                    obj = GameObject.Instantiate(Prefab as GameObject);
                else
                    obj = InstanceList.First.Value;
            }
            else
            {
                //这里用同步的原因是，在掉用对象池之前，已经把asset加载到内存了，用异步的方式
                AssetHandle assetHandle = GameEntry.AssetsLoader.GetExitAssetRefEntity(assetFullName).AssetHandler;
                
                if (assetHandle == null)
                {
                    GameEntry.LogError("加载的资源的handle不存在 :: " + assetFullName);
                    return null;
                }

                Prefab = assetHandle.AssetObject as GameObject;
                if (Prefab == null)
                {
                    GameEntry.LogError("加载的资源不存在 :: " + assetFullName);
                    return null;
                }
                else
                {
                    obj = GameObject.Instantiate(Prefab as GameObject);
                }
            }
            GameEntry.Log("xxl obj :: " + obj.name + " parent :: " + parent.name);
            obj.transform.SetParent(parent);
            obj.SetActive(true);
            return obj;
        }

        /// <summary>
        /// 回池
        /// </summary>
        /// <param name="namePath"></param>
        /// <param name="instanceComplete"></param>
        public void Enspawn(GameObject obj)
        {
            if (obj == null)
                return;
            InstanceList.AddLast(obj);
            obj.transform.SetParent(GameEntry.Pool.GameObjectPoosParent.transform);
            obj.SetActive(false);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Release()
        {
            DestroyInstance();
            Prefab = null;
        }

        /// <summary>
        /// 销毁实例化的 物体
        /// </summary>
        private void DestroyInstance()
        {
            LinkedListNode<GameObject> currLoad = InstanceList.First;
            while (currLoad != null)
            {
                if (currLoad.Value != null)
                {
                    GameObject.Destroy(currLoad.Value);
                    currLoad.Value = null;
                }
                currLoad = currLoad.Next;
            }
            InstanceList.Clear();
        }

    }
}
